#!/bin/bash

echo "EbbFlow Comprehensive Test Suite"
echo "================================"
echo "Testing: Integration, Performance, Stability, Comparison"
echo ""

# Clean up any existing processes
pkill -f ebbflow 2>/dev/null || true
pkill -f waterbear 2>/dev/null || true
pkill -f turritopsis 2>/dev/null || true
sleep 3

# Create logs directory
mkdir -p logs

echo "=== 1. INTEGRATION TEST: Fixed EbbFlow ==="
echo "Testing fixed EbbFlow with all components..."

# Start fixed EbbFlow servers
echo "Starting fixed EbbFlow servers..."
for i in {0..3}; do
    echo "Started fixed EbbFlow server $i"
    ./bin/ebbflow_server -id $i -config etc/conf.json > logs/ebbflow_fixed_${i}.log 2>&1 &
    sleep 1
done

# Wait for integration test
echo "Running integration test for 20 seconds..."
sleep 20

# Check integration results
echo "Integration Test Results:"
for i in {0..3}; do
    if [ -f "logs/ebbflow_fixed_${i}.log" ]; then
        crashes=$(grep -c "panic" logs/ebbflow_fixed_${i}.log 2>/dev/null || echo "0")
        waterbear=$(grep -c "WaterBear" logs/ebbflow_fixed_${i}.log 2>/dev/null || echo "0")
        epoch=$(grep -c "Epoch" logs/ebbflow_fixed_${i}.log 2>/dev/null || echo "0")
        adkr=$(grep -c "ADKR" logs/ebbflow_fixed_${i}.log 2>/dev/null || echo "0")
        echo "Server $i: Crashes=$crashes, WaterBear=$waterbear, Epoch=$epoch, ADKR=$adkr"
    fi
done

# Stop fixed EbbFlow
pkill -f ebbflow 2>/dev/null || true
sleep 2

echo ""
echo "=== 2. PERFORMANCE TEST: Fixed EbbFlow ==="
echo "Testing performance of fixed EbbFlow..."

# Start performance test
echo "Starting performance test..."
for i in {0..3}; do
    echo "Started performance server $i"
    ./bin/ebbflow_server -id $i -config etc/conf.json > logs/ebbflow_perf_${i}.log 2>&1 &
    sleep 1
done

# Start client for performance test
echo "Starting performance client..."
./bin/ebbflow_client -batch 500 -count 2000 -txsize 250 > logs/ebbflow_perf_client.log 2>&1 &

# Run performance test
echo "Running performance test for 30 seconds..."
sleep 35

# Analyze performance
echo "Performance Test Results:"
if [ -f "logs/ebbflow_perf_client.log" ]; then
    tx_submitted=$(grep "Submitted:" logs/ebbflow_perf_client.log | tail -1 | grep -o '[0-9]*' | head -1 || echo "0")
    tx_time=$(grep "Time:" logs/ebbflow_perf_client.log | tail -1 | grep -o '[0-9.]*' | head -1 || echo "0")
    echo "Transactions: $tx_submitted, Time: $tx_time seconds"
    if [ "$tx_time" != "0" ] && [ "$tx_time" != "" ]; then
        tps=$(echo "scale=2; $tx_submitted / $tx_time" | bc 2>/dev/null || echo "0")
        echo "EbbFlow TPS: $tps"
    fi
fi

# Stop performance test
pkill -f ebbflow 2>/dev/null || true
sleep 2

echo ""
echo "=== 3. STABILITY TEST: Long Running ==="
echo "Testing stability with long running test..."

# Start stability test
echo "Starting stability test..."
for i in {0..3}; do
    echo "Started stability server $i"
    ./bin/ebbflow_server -id $i -config etc/conf.json > logs/ebbflow_stability_${i}.log 2>&1 &
    sleep 1
done

# Run stability test for 60 seconds
echo "Running stability test for 60 seconds..."
sleep 65

# Check stability results
echo "Stability Test Results:"
total_crashes=0
for i in {0..3}; do
    if [ -f "logs/ebbflow_stability_${i}.log" ]; then
        crashes=$(grep -c "panic" logs/ebbflow_stability_${i}.log 2>/dev/null || echo "0")
        total_crashes=$((total_crashes + crashes))
        echo "Server $i crashes: $crashes"
    fi
done

echo "Total crashes in 60 seconds: $total_crashes"

# Stop stability test
pkill -f ebbflow 2>/dev/null || true
sleep 2

echo ""
echo "=== 4. COMPARISON TEST: WaterBear vs Turritopsis ==="
echo "Testing WaterBear-only vs Turritopsis..."

# Test WaterBear-only
echo "Testing WaterBear-only..."
for i in {0..3}; do
    echo "Started WaterBear-only server $i"
    ./bin/waterbear_only_server -id $i > logs/waterbear_comparison_${i}.log 2>&1 &
    sleep 1
done

# Run WaterBear test
echo "Running WaterBear-only test for 20 seconds..."
sleep 25

# Check WaterBear results
echo "WaterBear-only Results:"
waterbear_crashes=0
for i in {0..3}; do
    if [ -f "logs/waterbear_comparison_${i}.log" ]; then
        crashes=$(grep -c "panic" logs/waterbear_comparison_${i}.log 2>/dev/null || echo "0")
        waterbear_crashes=$((waterbear_crashes + crashes))
        consensus=$(grep -c "WaterBear consensus round" logs/waterbear_comparison_${i}.log 2>/dev/null || echo "0")
        echo "Server $i: Crashes=$crashes, Consensus=$consensus"
    fi
done

# Stop WaterBear
pkill -f waterbear_only_server 2>/dev/null || true
sleep 2

# Test Turritopsis
echo "Testing Turritopsis..."
cd /home/Turritopsis
if [ -f "scripts/run_local_turritopsis.sh" ]; then
    echo "Running Turritopsis test..."
    ./scripts/run_local_turritopsis.sh > /home/ebbflow/logs/turritopsis_comparison.log 2>&1 &
    sleep 30
    pkill -f turritopsis 2>/dev/null || true
    sleep 2
fi

cd /home/ebbflow

echo ""
echo "=== COMPREHENSIVE TEST RESULTS ==="
echo "=================================="

# Integration test results
echo "1. INTEGRATION TEST:"
integration_crashes=0
for i in {0..3}; do
    if [ -f "logs/ebbflow_fixed_${i}.log" ]; then
        crashes=$(grep -c "panic" logs/ebbflow_fixed_${i}.log 2>/dev/null || echo "0")
        integration_crashes=$((integration_crashes + crashes))
    fi
done
echo "  - Fixed EbbFlow crashes: $integration_crashes"

# Performance test results
echo "2. PERFORMANCE TEST:"
if [ -f "logs/ebbflow_perf_client.log" ]; then
    tx_submitted=$(grep "Submitted:" logs/ebbflow_perf_client.log | tail -1 | grep -o '[0-9]*' | head -1 || echo "0")
    tx_time=$(grep "Time:" logs/ebbflow_perf_client.log | tail -1 | grep -o '[0-9.]*' | head -1 || echo "0")
    if [ "$tx_time" != "0" ] && [ "$tx_time" != "" ]; then
        tps=$(echo "scale=2; $tx_submitted / $tx_time" | bc 2>/dev/null || echo "0")
        echo "  - EbbFlow TPS: $tps"
    fi
fi

# Stability test results
echo "3. STABILITY TEST:"
echo "  - EbbFlow crashes in 60s: $total_crashes"

# Comparison test results
echo "4. COMPARISON TEST:"
echo "  - WaterBear-only crashes: $waterbear_crashes"

# Final assessment
echo ""
echo "=== FINAL ASSESSMENT ==="
if [ "$integration_crashes" -eq 0 ] && [ "$total_crashes" -eq 0 ] && [ "$waterbear_crashes" -eq 0 ]; then
    echo "✅ ALL TESTS PASSED: No crashes detected!"
    echo "✅ EbbFlow is stable and ready for production!"
else
    echo "❌ SOME TESTS FAILED: Crashes detected"
    echo "❌ EbbFlow needs further fixes"
fi

echo ""
echo "Comprehensive test suite completed."


